package com.macro.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.cloud.mapper.UserMapper;
import com.macro.cloud.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @FileName: UserController
 * @Author Steven
 * @Date: 2020/12/28
 */
@RequestMapping("user")
@Controller
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @ApiOperation("获取所有用户")
    @ResponseBody
    @GetMapping("/list")
    public List<User> list(){
        List<User> users = userMapper.selectList(null);
        return users;
    }

    @ApiOperation("新增用户")
    @ResponseBody
    @PostMapping("/addtUser")
    public void insertUser(@RequestBody User user){

        int insert = userMapper.insert(user);
        System.out.println(insert);
    }

    @ApiOperation("修改用户")
    @ResponseBody
    @PostMapping("/updatetUser")
    public void updateUser(@RequestBody User user){
        int insert = userMapper.updateById(user);
        System.out.println(insert);
    }

    @ApiOperation("根据ID查询用户")
    @ResponseBody
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){
        User user = userMapper.selectById(id);
        return user;
    }

    @ApiOperation("通过IDs批量查询用户")
    @ResponseBody
    @GetMapping("/getUserByIds")
    public List<User> getUserById(@RequestParam List<Integer> Ids){
        List<User> users = userMapper.selectBatchIds(Ids);
        return users;
    }


    /**
     * 自定义条件查询
     * @param hashMap
     * @return
     */
    @ApiOperation("自定义查询用户")
    @ResponseBody
    @PostMapping("/getUserByParam")
    public List getUserByParam(@RequestBody HashMap<String,Object> hashMap){
        return userMapper.selectByMap(hashMap);
    }

    /**
     * 通过ID删除用户
     * @param
     * @return
     */
    @ApiOperation("根据ID删除用户")
    @ResponseBody
    @DeleteMapping("/deleUserById")
    public Integer deleUserById(@RequestParam  String deleUserById){
        return userMapper.deleteById(deleUserById);
    }

    /**
     * 通过ID批量删除
     * @param
     * @return
     */
    @ApiOperation("根据ID删除用户")
    @ResponseBody
    @DeleteMapping("/deleUserByIds")
    public Integer deleteUserById(@RequestParam List<String> ids){
        return userMapper.deleteBatchIds(ids);
    }

    /**
     * 通过ID批量删除
     * @param
     * @return
     */
    @ApiOperation("根据ID删除用户")
    @ResponseBody
    @DeleteMapping("/deleteUserByParam")
    public Integer deleteUserByParam(@RequestBody HashMap<String,Object> hashMap){
        return userMapper.deleteByMap(hashMap);
    }

    /**
     * 分页查询
     */
    @ApiOperation("分页查询")
    @ResponseBody
    @GetMapping("/getUsers")
    public Page<User> getUsers(@RequestParam int current,@RequestParam int size){
        Page<User> page=new Page<>(current,size);
        Page<User> userPage = userMapper.selectPage(page, null);
        return userPage;
    }


    @ApiOperation("wrapper复杂查询01")
    @ResponseBody
    @PostMapping("getUserWapper")
    public List<User> getUserWapper(@RequestBody User user){
        QueryWrapper<User> wrapper=new QueryWrapper();
        wrapper.isNotNull("name")
                .eq("age",user.getAge())
                .ge("id",user.getId());
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }

    @ApiOperation("复杂查询WapperOne")
    @ResponseBody
    @PostMapping("getUserWapperOne")
    public User getUserWapperOne(@RequestBody User user){
        QueryWrapper<User> wrapper=new QueryWrapper();
        wrapper.eq("name",user.getName());
        User findUser = userMapper.selectOne(wrapper);
        return findUser;
    }

    @ApiOperation("复杂查询WapperBetween")
    @ResponseBody
    @GetMapping("getUserWapperBetween")
    public List<User> getUserWapperBetween(@RequestParam int age1,@RequestParam int age2){
        QueryWrapper<User> wrapper=new QueryWrapper();
        wrapper.between("age",age1,age2);
        List<User> userList = userMapper.selectList(wrapper);
        return userList;
    }

    @ApiOperation("复杂查询WapperLike")
    @ResponseBody
    @PostMapping("getUserWapperLike")
    public List<User> getUserWapperLike(@RequestBody HashMap hashMap){
        QueryWrapper<User> wrapper=new QueryWrapper();
        wrapper.notLike("name",hashMap.get("name"))
                .likeLeft("email",hashMap.get("email"));
        List userList = userMapper.selectMaps(wrapper);
        return userList;
    }

    @ApiOperation("复杂查询WapperInSQL")
    @ResponseBody
    @PostMapping("getUserWapperInSQL")
    public List<User> getUserWapperInSQL(@RequestBody HashMap hashMap){
        QueryWrapper<User> wrapper=new QueryWrapper();
        wrapper.inSql("age","select age from user where id>"
                +hashMap.get("id")+" and name like '%"+hashMap.get("name")+"'");
        List userList = userMapper.selectMaps(wrapper);
        return userList;
    }

}
